package com.itheima.controller;

import com.itheima.domain.Student;
import com.itheima.service.StudentService;

import java.util.Scanner;

public class StudentController {
    private StudentService stuSer=new StudentService();
    Scanner sc = new Scanner(System.in);
    public void start() {
        lo: while (true) {
            System.out.println("-------------欢迎来到<学生>管理系统-------------");
            System.out.println("请输入您的选择：1.添加学生 2.删除学生 3.查询学生 4.修改学生 5退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //添加学生
                    addStudent();
                    break;
                case "2":
                    //删除学生
                    deletStudent();
                    break;
                case "3":
                    //查询学生
                    selectStudent();
                    break;
                case "4":
                    //修改学生
                    updateStudent();
                    break;
                case "5":
                    System.out.println("退出");
                    break lo;
                default:
                    System.out.println("您输入的有误");
                    break;
            }

        }
    }

    private void updateStudent() {
        String sid;
        lo: while (true){
            System.out.println("请输入你要修改的学生学号");
            sid = sc.next();
            boolean exists=stuSer.isExists(sid);
            if(!exists){
                System.out.println("您输入的学号不存在，请重新输入：");
            }else {
                break lo;
            }
        }
        System.out.println("请输入学生的姓名");
        String name = sc.next();
        System.out.println("请输入学生的年龄");
        String age = sc.next();
        System.out.println("请输入学生的出生日期");
        String birthday = sc.next();
        Student stu=new Student();
        stu.setSid(sid);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        stuSer.updateStudent(sid,stu);
        System.out.println("修改成功！");
    }

    public void deletStudent() {
        String sid;
       lo: while (true){
                System.out.println("请输入你要删除的学生学号");
                sid = sc.next();
                boolean exists=stuSer.isExists(sid);
                if(!exists){
                    System.out.println("您输入的学号不存在，请重新输入：");
                }else {
                break lo;
            }
        }
        stuSer.deletStudent(sid);
        System.out.println("删除成功！");
    }

    public void selectStudent() {
        Student[] stus =stuSer.selectStudent();
        if(stus==null){
            System.out.println("查无信息，请添加后再查询。");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t出生日期");
        for (int i = 0; i < stus.length; i++) {
            Student stu=stus[i];
            if(stu!=null){
                System.out.println(stu.getSid()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t\t"+stu.getBirthday());
            }

        }
    }

    public void addStudent() {
        String sid;
        lo: while (true){
            System.out.println("请输入学生的学号");
             sid = sc.next();
            boolean flag= stuSer.isExists(sid);
            if(flag){
                System.out.println("学号已存在，请重新输入");
            }else {
                break lo;
            }
        }
        System.out.println("请输入学生的姓名");
        String name = sc.next();
        System.out.println("请输入学生的年龄");
        String age = sc.next();
        System.out.println("请输入学生的出生日期");
        String birthday = sc.next();
        Student stu=new Student();
        stu.setSid(sid);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
         boolean result =stuSer.addStudent(stu);
         if(result){
             System.out.println("添加成功");
         }else {
             System.out.println("添加失败");
         }
    }
}
